package com.example.common.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.example.common.utils.StringUtils;

/**
 * @Author 王鹤蒲
 * @Date 2023 10 14
 */
public class StatusConverter implements Converter<Integer> {
    public Class<?> supportJavaTypeKey() {
        return Integer.class;
    }

    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }

    public Integer convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
         String value=cellData.getStringValue();
         if(StringUtils.isEmpty(value)){
            return null;
         }
         if("正常".equals(value)){
              return 1;
         }else if("停用".equals(value)){
              return 0;
         }else {
              return null;
         }
    }

    public WriteCellData convertToExcelData(Integer value, ExcelContentProperty excelContentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value== null) {
            return new WriteCellData<>("");
        }
        if (value == 0) {
            return new WriteCellData<>("停用");
        } else if (value == 1) {
            return new WriteCellData<>("正常");
        } else {
            return new WriteCellData<>("");
        }
    }



}
